package com.edu.exam.entity.grading;

import com.edu.exam.enums.common.WorkflowStatus;
import com.edu.exam.enums.grading.ImageDisplayMode;
import com.edu.exam.enums.grading.SessionStatus;
import com.edu.exam.enums.grading.UiTheme;
import com.edu.exam.enums.grading.SessionHealthStatus;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

import java.time.LocalDateTime;

/**
 * 网上批阅会话实体类
 * 所属模块：B-06 成绩管理模块
 *
 * 根据E-04-05 网上批阅子模块设计实现
 * 对应grading_session数据表
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
public class GradingSession {

    @Id
    @Column(name = "session_id", length = 64)
    private String sessionId;

    /**
     * 任务ID
     */
    @Column(name = "task_id", nullable = false, length = 64)
    private String taskId;

    /**
     * 教师ID
     */
    @Column(name = "teacher_id", nullable = false, length = 64)
    private String teacherId;

    /**
     * 会话状态：ACTIVE（活跃）、INACTIVE（非活跃）、EXPIRED（已过期）、TERMINATED（已终止）
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "session_status", nullable = false, length = 32)
    private SessionStatus sessionStatus;

    /**
     * 开始时间
     */
    @Column(name = "start_time", nullable = false)
    private LocalDateTime startTime;

    /**
     * 结束时间
     */
    @Column(name = "end_time")
    private LocalDateTime endTime;

    /**
     * 最后活动时间
     */
    @Column(name = "last_activity_time", nullable = false)
    private LocalDateTime lastActivityTime;

    /**
     * 当前图像ID
     */
    @Column(name = "current_image_id", length = 64)
    private String currentImageId;

    /**
     * 会话配置（JSON格式）
     */
    @Column(name = "session_config", length = 2000, columnDefinition = "TEXT")
    private String sessionConfig;

    /**
     * 浏览器信息
     */
    @Column(name = "browser_info", length = 512)
    private String browserInfo;

    /**
     * IP地址
     */
    @Column(name = "ip_address", length = 64)
    private String ipAddress;

    /**
     * 用户代理信息
     */
    @Column(name = "user_agent", length = 500)
    private String userAgent;

    /**
     * 屏幕分辨率
     */
    @Column(name = "screen_resolution", length = 32)
    private String screenResolution;

    /**
     * 时区信息
     */
    @Column(name = "timezone", length = 64)
    private String timezone;

    /**
     * 语言设置
     */
    @Column(name = "language", length = 16)
    private String language;

    /**
     * 自动保存间隔（秒）
     */
    @Column(name = "auto_save_interval")
    private Integer autoSaveInterval;

    /**
     * 会话超时时间（分钟）
     */
    @Column(name = "session_timeout_minutes")
    private Integer sessionTimeoutMinutes;

    /**
     * 最大空闲时间（分钟）
     */
    @Column(name = "max_idle_time_minutes")
    private Integer maxIdleTimeMinutes;

    /**
     * 是否启用自动保存
     */
    @Column(name = "auto_save_enabled")
    private Boolean autoSaveEnabled;

    /**
     * 是否启用键盘快捷键
     */
    @Column(name = "keyboard_shortcuts_enabled")
    private Boolean keyboardShortcutsEnabled;

    /**
     * 是否启用声音提示
     */
    @Column(name = "sound_notifications_enabled")
    private Boolean soundNotificationsEnabled;

    /**
     * 界面主题：LIGHT（浅色）、DARK（深色）、AUTO（自动）
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "ui_theme", length = 16)
    private UiTheme uiTheme;

    /**
     * 界面缩放比例
     */
    @Column(name = "ui_scale")
    private Double uiScale;

    /**
     * 图像显示模式：FIT_TO_SCREEN（适应屏幕）、ACTUAL_SIZE（实际大小）、CUSTOM_SCALE（自定义缩放）
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "image_display_mode", length = 32)
    private ImageDisplayMode imageDisplayMode;

    /**
     * 批注工具栏设置（JSON格式）
     */
    @Column(name = "annotation_toolbar_config", length = 1000, columnDefinition = "TEXT")
    private String annotationToolbarConfig;

    /**
     * 快捷键配置（JSON格式）
     */
    @Column(name = "shortcut_config", length = 1000, columnDefinition = "TEXT")
    private String shortcutConfig;

    /**
     * 会话统计信息（JSON格式）
     */
    @Column(name = "session_statistics", length = 2000, columnDefinition = "TEXT")
    private String sessionStatistics;

    /**
     * 错误日志（JSON格式）
     */
    @Column(name = "error_log", length = 2000, columnDefinition = "TEXT")
    private String errorLog;

    /**
     * 性能监控数据（JSON格式）
     */
    @Column(name = "performance_data", length = 2000, columnDefinition = "TEXT")
    private String performanceData;

    /**
     * 会话备注
     */
    @Column(name = "session_notes", length = 1000, columnDefinition = "TEXT")
    private String sessionNotes;

    /**
     * 上次保存时间
     */
    @Column(name = "last_save_time")
    private LocalDateTime lastSaveTime;

    /**
     * 保存次数
     */
    @Column(name = "save_count")
    private Integer saveCount;

    /**
     * 异常退出次数
     */
    @Column(name = "abnormal_exit_count")
    private Integer abnormalExitCount;

    /**
     * 最后异常时间
     */
    @Column(name = "last_abnormal_exit_time")
    private LocalDateTime lastAbnormalExitTime;

    /**
     * 扩展属性（JSON格式）
     */
    @Column(name = "additional_attributes", length = 2000, columnDefinition = "TEXT")
    private String additionalAttributes;

    /**
     * 创建时间
     */
    @Column(name = "created_time", nullable = false)
    private LocalDateTime createdTime;

    /**
     * 更新时间
     */
    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    /**
     * 版本号（用于乐观锁）
     */
    @Version
    @Column(name = "version", nullable = false)
    private Integer version;

    // ================================
    // JPA 关联关系
    // ================================

    /**
     * 关联的批阅任务
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "task_id", insertable = false, updatable = false)
    private OnlineGradingTask gradingTask;

    // ================================
    // 枚举已迁移
    // ================================
    // 嵌入枚举已迁移到专用枚举包:
    // - SessionStatus -> SessionStatus (enums/grading包)
    // - UiTheme -> UiTheme (enums/grading包)
    // - ImageDisplayMode -> ImageDisplayMode (enums/grading包)
}